package demoMod.invincibleOne.patches;

import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.powers.RegenerateMonsterPower;
import com.megacrit.cardcrawl.powers.StrengthPower;
import demoMod.invincibleOne.cards.invincible.BigIsBetter;
import demoMod.invincibleOne.interfaces.MonsterRegenOrMaxHPApplyHook;

public class AbstractPowerPatch {
    @SpirePatch(
            clz = AbstractPower.class,
            method = "stackPower"
    )
    public static class PatchStackPower {
        public static void Postfix(AbstractPower power, int stackAmount) {
            if (power instanceof RegenerateMonsterPower && stackAmount > 0) {
                float tmp = stackAmount;
                for (AbstractPower power1 : power.owner.powers) {
                    if (power1 instanceof MonsterRegenOrMaxHPApplyHook) {
                        tmp = ((MonsterRegenOrMaxHPApplyHook) power1).onRegenApply(power.owner, tmp);
                    }
                }
                for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                    if (card instanceof MonsterRegenOrMaxHPApplyHook) {
                        tmp = ((MonsterRegenOrMaxHPApplyHook) card).onRegenApply(power.owner, tmp);
                    }
                }
                power.amount -= stackAmount;
                power.amount += (int) Math.ceil(tmp);
            }
            if (power instanceof StrengthPower && stackAmount > 0 && power.owner instanceof AbstractMonster) {
                float tmp = stackAmount;
                for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                    if (card instanceof BigIsBetter) {
                        tmp *= 1.0F + card.magicNumber / 100.0F;
                    }
                }
                power.amount -= stackAmount;
                power.amount += (int) Math.ceil(tmp);
            }
        }
    }

    @SpirePatch(
            clz = AbstractPower.class,
            method = "onInitialApplication"
    )
    public static class PatchOnInitialApplication {
        public static void Postfix(AbstractPower power) {
            if (power instanceof RegenerateMonsterPower && power.amount > 0) {
                float tmp = power.amount;
                for (AbstractPower power1 : power.owner.powers) {
                    if (power1 instanceof MonsterRegenOrMaxHPApplyHook) {
                        tmp = ((MonsterRegenOrMaxHPApplyHook) power1).onRegenApply(power.owner, tmp);
                    }
                }
                for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                    if (card instanceof MonsterRegenOrMaxHPApplyHook) {
                        tmp = ((MonsterRegenOrMaxHPApplyHook) card).onRegenApply(power.owner, tmp);
                    }
                }
                power.amount = (int) Math.ceil(tmp);
            }
            if (power instanceof StrengthPower && power.amount > 0 && power.owner instanceof AbstractMonster) {
                float tmp = power.amount;
                for (AbstractCard card : AbstractDungeon.player.masterDeck.group) {
                    if (card instanceof BigIsBetter) {
                        tmp *= 1.0F + card.magicNumber / 100.0F;
                    }
                }
                power.amount = (int) Math.ceil(tmp);
            }
        }
    }
}
